from flask import Blueprint,request,jsonify
import time
from model.model import *

house=Blueprint('/house',__name__)
from Redis import temp_cache


@temp_cache.cached(timeout=10)
@house.route('/house')
def Home_Get():
    print(time.time())
    time.sleep(2)
    start_time=time.time()
    home=Hoser.query.filter_by().all()
    print(time.time()-start_time)
    aaa=[]
    for i in home:
        aaa.append({
            'id':i.id,
            "name":i.name,
            "show":i.show
        })

    return jsonify(code=200,msg=aaa)



@house.route('/house',methods=['post'])
def Home_Add():
    data=request.json
    data.pop('pid')
    home=Hoser(**data)
    db.session.add(home)
    db.session.commit()
    return jsonify(code=200,msg=data)


@house.route('/house',methods=['put'])
def Home_Put():
    data=request.json
    p_id=data.pop('pid')
    # p_id=data.get('pid')
    # name=data.get('name')
    # show=data.get('show')
    # Hoser.query.filter(Hoser.id==p_id).update({'name':name,'show':show})
    home=Hoser.query.filter(Hoser.id==p_id).update(dict(**data))
    db.session.commit()


    return jsonify(code=200,msg='成功',data=home)
#
@house.route('/house',methods=['delete'])
def Home_Delete():
    p_id=request.json.get('pid')
    home=Hoser.query.filter_by(id=p_id).first()
    db.session.delete(home)
    db.session.commit()
    return jsonify(code=200,msg='成功')
